package www.golf.web.util;

import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Button;
import org.zkoss.zul.Captcha;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Decimalbox;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Radiogroup;
import org.zkoss.zul.Row;
import org.zkoss.zul.Spinner;
import org.zkoss.zul.Textbox;


/**
 * zk util get value
 * @author angle
 * @since 2009/11/26
 * @version 1.2
 */
public class UtilZKObject {
	Component com=null;
	public UtilZKObject(Component com){
		this.com=com;
	}
	public Component getContrain(){
		return com;
	}
	public String getAttributeValue(String id,String Attribute_name){
		return getObjectAttribute(this.com.getFellow(id), Attribute_name);
	}
	public String getValue(String id){
		return getObjectValue(this.com.getFellow(id));
	}
	public void clearValue(String id){
		clearObjectValue(this.com.getFellow(id));
	}
	public static void clearObjectValue(Object obj) {
		if(obj instanceof Datebox){
			((Datebox)obj).setText("");
		}else if(obj instanceof Textbox){
			((Textbox)obj).setText("");
		}else if(obj instanceof Label){
			((Label)obj).setValue("");
		}else if(obj instanceof Listbox){
			Listbox tempList=((Listbox)obj);
			for(int a=tempList.getItemCount()-1;a>=0;a--) tempList.removeItemAt(a);			
		}else if(obj instanceof Checkbox){
			((Checkbox)obj).setChecked(false);
		}else if(obj instanceof Intbox){
			((Intbox)obj).setText("0");
		}else if(obj instanceof Doublebox){
			((Doublebox)obj).setText("0");
		}else if(obj instanceof Decimalbox){			
		}else if(obj instanceof Spinner){			
		}else if(obj instanceof Captcha){			
		}else System.out.println("The Object no support!!");
	}
	
	public static String getObjectValue(Object obj) {
		String tempStr="";
		if(obj instanceof Datebox){
//			tempStr=((new Date(((Datebox) obj).getValue().getTime()))).toString();
			tempStr=((Datebox)obj).getText().trim();
		}else if(obj instanceof Textbox){
			tempStr=((Textbox)obj).getText().trim();
			return strManage.changeString(tempStr,false);
		}else if(obj instanceof Label){
			tempStr=(((Label)obj).getValue()+"").trim();
		}else if(obj instanceof Listbox){
			if(((Listbox)obj).getItemCount()>0){
				if(((Listbox)obj).getSelectedItem()==null) ((Listbox)obj).setSelectedIndex(0); 
				tempStr=((Listbox)obj).getSelectedItem().getValue()+"";
			}else tempStr="";
		}else if(obj instanceof Radiogroup){
			tempStr=((Radiogroup)obj).getSelectedItem().getValue()+"";			
		}else if(obj instanceof Listcell){
			tempStr=((Listcell)obj).getLabel();			
		}else if(obj instanceof Checkbox){
			tempStr=((Checkbox)obj).isChecked()?"1":"0";
		}else if(obj instanceof Intbox){
			tempStr=((Intbox)obj).getText().trim();
		}else if(obj instanceof Decimalbox){
			tempStr=((Decimalbox)obj).getText().trim();
		}else if(obj instanceof Spinner){
			tempStr=((Spinner)obj).getText().trim();
		}else if(obj instanceof Captcha){
			tempStr=((Captcha)obj).getValue().trim();			
		}else if(obj instanceof Button){
			tempStr=((Button)obj).getLabel().trim();
		}else if(obj instanceof Doublebox){
			tempStr=((Doublebox)obj).getText().trim();	
		}else{
			System.out.println("The Object no support!!");
			return "The Object no support!!";
		}
		return strManage.changeString(tempStr);
		//return tempStr;		
	}	
	
	public static String getObjectAttribute(Object obj,String attributeName) {
		String tempStr="";		
		if(obj instanceof Datebox){
			tempStr = (((Datebox)obj).getAttribute(attributeName)+"").trim();
		}else if(obj instanceof Textbox){
			tempStr = (((Textbox)obj).getAttribute(attributeName)+"").trim();
		}else if(obj instanceof Listbox){
			tempStr = (((Listbox)obj).getSelectedItem().getAttribute(attributeName)+"").trim();			
		}else if(obj instanceof Listitem){
			tempStr = (((Listitem)obj).getAttribute(attributeName)+"").trim();			
		}else if(obj instanceof Label){
			tempStr = (((Label) obj).getAttribute(attributeName)+"").trim();
		}else if(obj instanceof Checkbox){
			tempStr = ((Checkbox)obj).getAttribute(attributeName)+"";
		}else if(obj instanceof Intbox){			
			tempStr = ((Intbox)obj).getAttribute(attributeName)+"";
		}else if(obj instanceof Doublebox){			
			tempStr = ((Doublebox)obj).getAttribute(attributeName)+"";
		}else if(obj instanceof Decimalbox){			
			tempStr = ((Decimalbox)obj).getAttribute(attributeName)+"";
		}else if(obj instanceof Spinner){			
			tempStr = ((Spinner)obj).getAttribute(attributeName)+"";
		}else if(obj instanceof Captcha){			
			tempStr = ((Captcha)obj).getAttribute(attributeName)+"";
		}else if(obj instanceof Radiogroup){
			tempStr=((Radiogroup)obj).getSelectedItem().getAttribute(attributeName)+"";			
		}else tempStr = "The Object no support!!";
		return strManage.changeString(tempStr);
	}	
	
	public static Object setObjectAttribute(Object obj,String attributeName,Object attributeValue) {
		if(attributeValue instanceof String) attributeValue=strManage.changeString(attributeValue+"");
		if(obj instanceof Datebox){
			((Datebox)obj).setAttribute(attributeName,attributeValue);
		}else if(obj instanceof Textbox){
			((Textbox)obj).setAttribute(attributeName,attributeValue);
		}else if(obj instanceof Listbox){
			((Listbox)obj).getSelectedItem().setAttribute(attributeName,attributeValue);			
		}else if(obj instanceof Listitem){
			((Listitem)obj).setAttribute(attributeName,attributeValue);			
		}else if(obj instanceof Label){
			((Label) obj).setAttribute(attributeName,attributeValue);
		}else if(obj instanceof Checkbox){
			((Checkbox)obj).setAttribute(attributeName,attributeValue);
		}else if(obj instanceof Intbox){			
			((Intbox)obj).setAttribute(attributeName,attributeValue);
		}else if(obj instanceof Doublebox){			
			((Doublebox)obj).setAttribute(attributeName,attributeValue);
		}else if(obj instanceof Decimalbox){			
			((Decimalbox)obj).setAttribute(attributeName,attributeValue);
		}else if(obj instanceof Spinner){			
			((Spinner)obj).setAttribute(attributeName,attributeValue);
		}else if(obj instanceof Captcha){			
			((Captcha)obj).setAttribute(attributeName,attributeValue);
		}else System.out.println("The Object no support!!");
		return obj;
	}	
	
	public static String getObjectLabel(Object obj) {
		String tempStr = "";
		if(obj instanceof Listbox){
			tempStr = ((Listbox)obj).getSelectedItem().getLabel()+"";
		}else if(obj instanceof Radiogroup){
			tempStr = ((Radiogroup)obj).getSelectedItem().getLabel()+"";			
		}else if(obj instanceof Checkbox){
			tempStr = ((Checkbox)obj).getLabel();
		}else if(obj instanceof Button){
			tempStr = ((Button)obj).getLabel();			
		}else if(obj instanceof Label){
			tempStr = ((Label)obj).getValue()+"";			
		}else tempStr = "The Object no support!!";
		return strManage.changeString(tempStr);
	}	

	public static void setObjectLabel(Object obj,String value) {

		if(obj instanceof Listbox){
			((Listbox)obj).getSelectedItem().setLabel(value);
		}else if(obj instanceof Radiogroup){
			((Radiogroup)obj).getSelectedItem().setLabel(value);			
		}else if(obj instanceof Checkbox){
			((Checkbox)obj).setLabel(value);
		}else if(obj instanceof Button){
			((Button)obj).setLabel(value);			
		}else if(obj instanceof Label){
			((Label)obj).setValue(value);			
		}else System.out.println("The Object no support!!");
	}
	
	public static void setObjectMaxLength(Object obj,int maxlength) {
		if(obj instanceof Datebox){		
			((Datebox)obj).setMaxlength(maxlength);
		}else if(obj instanceof Textbox){		
			((Textbox)obj).setMaxlength(maxlength);
		}else if(obj instanceof Label){
			((Label)obj).setMaxlength(maxlength);
		}else if(obj instanceof Listbox){
			((Listbox)obj).setMaxlength(maxlength);			
		}else if(obj instanceof Intbox){		
			((Intbox)obj).setMaxlength(maxlength);
		}else if(obj instanceof Doublebox){		
			((Doublebox)obj).setMaxlength(maxlength);
		}else if(obj instanceof Decimalbox){		
			((Decimalbox)obj).setMaxlength(maxlength);
		}else if(obj instanceof Spinner){			
			((Spinner)obj).setMaxlength(maxlength);
		}else System.out.println("The Object no support [MaxLength]!!");			
	}
	public Object setObjectValue(String id,Object fieldValue) {
		if(this.com.getFellowIfAny(id)==null){
			return null;
		}
		return setObjectValue(this.com.getFellow(id),fieldValue);
	}
	public static Object setObjectValue(Object obj,Object fieldValue) {
		if((fieldValue+"").equals("null")) fieldValue="";
		String tempStr=strManage.reChangeString(fieldValue+"");
		if(obj instanceof Datebox){		
			((Datebox)obj).setText(!tempStr.equals("null")?tempStr.trim():"");
		}else if(obj instanceof Textbox){		
			((Textbox)obj).setValue(!tempStr.equals("null")?tempStr.trim():"");
		}else if(obj instanceof Label){
			((Label)obj).setValue(!tempStr.equals("null")?tempStr.trim():"");
		}else if(obj instanceof Listbox){
			for(int a=0;a<((Listbox)obj).getItemCount();a++){
				Listitem tempItem=(Listitem)((Listbox)obj).getItemAtIndex(a);				
				if((tempItem.getValue()+"").equals((fieldValue+""))){
					tempItem.setSelected(true);
					break;
				}
			}
		}else if(obj instanceof Radiogroup){
			for(int a=0;a<((Radiogroup)obj).getItemCount();a++){
				Radio tempRadio=(Radio)((Radiogroup)obj).getItemAtIndex(a);
				if((tempRadio.getValue()+"").equals((fieldValue+""))){
					tempRadio.setChecked(true);
					break;
				}
			}			
		}else if(obj instanceof Checkbox){
			boolean tempBoolean=true;
			int mode=0;
			if(fieldValue  instanceof String){
				if(((String)fieldValue).equals("1")) tempBoolean=true;
				else if(((String)fieldValue).equals("0")) tempBoolean=false;
				else mode=1;
			}else if(fieldValue  instanceof Integer){
				if(Integer.parseInt(fieldValue+"")==1) tempBoolean=true;
				else if(Integer.parseInt(fieldValue+"")==2) tempBoolean=false;
				else mode=1;
			}else if(fieldValue  instanceof Boolean){
				tempBoolean=Boolean.parseBoolean(fieldValue+"");
			}
			if(mode==0) ((Checkbox)obj).setChecked(tempBoolean);
		}else if(obj instanceof Intbox){		
			((Intbox)obj).setValue(!tempStr.equals("null")?Integer.parseInt(tempStr.trim()):0);
		}else if(obj instanceof Decimalbox){		
			((Decimalbox)obj).setText(!tempStr.equals("null")?tempStr.trim():"");
		}else if(obj instanceof Spinner){			
			((Spinner)obj).setValue(!tempStr.equals("null")?Integer.parseInt(tempStr.trim()):0);
		}else if(obj instanceof Captcha){
			((Captcha)obj).setValue(!tempStr.equals("null")?tempStr.trim():"");
		}else if(obj instanceof Button){
			((Button)obj).setLabel(!tempStr.equals("null")?tempStr.trim():"");			
		}else if(obj instanceof Doublebox){		
			((Doublebox)obj).setValue(!tempStr.equals("null")?Double.parseDouble(tempStr.trim()):0);
		}
		return obj;
	}	
	
	public static Listcell setObjectValue(Listitem item,Object cellValue) {
		return setObjectValue(item,cellValue,true);
	}
	//憒�憿���閰脩隞��dd2Item�喳False,��銝�add�彪ell銝��垢�閰脩隞嗅��
	//P.S add2Item�喳False�����芾�撠府�拐辣�Cell銝��身�箄��閰脩隞嗅��丘ell銝�	
	public static Listcell setObjectValue(Listitem item,Object cellValue,boolean add2Item) {
		String tempStr=strManage.reChangeString(cellValue+"");		
		Listcell tempCell = new Listcell();
		tempCell.setLabel(!tempStr.equals("null")?tempStr.trim():"");
		if(add2Item) tempCell.setParent(item);
		return tempCell;
	}

	public static Listcell addObjectChild(Listitem item,Component obj) {
		return addObjectChild(item,obj,true);
	}
	//憒�憿���閰脩隞��dd2Item�喳False,��銝�add�彪ell銝��垢�閰脩隞嗅��
	//P.S add2Item�喳False�����芾�撠府�拐辣�Cell銝��身�箄��閰脩隞嗅��丘ell銝�	
	public static Listcell addObjectChild(Listitem item,Component obj,boolean add2Item) {	
		Listcell tempCell = new Listcell();
		tempCell.appendChild(obj);
		if(add2Item) tempCell.setParent(item);
		return tempCell;
	}	
	
	public static void setObjectVisible(Object obj,boolean bool){
		if(obj instanceof Datebox){
			((Datebox)obj).setVisible(bool);
		}else if(obj instanceof Textbox){
			((Textbox)obj).setVisible(bool);
		}else if(obj instanceof Label){
			((Label)obj).setVisible(bool);			
		}else if(obj instanceof Listbox){
			((Listbox)obj).setVisible(bool);			
		}else if(obj instanceof Radio){
			((Radio)obj).setVisible(bool);			
		}else if(obj instanceof Checkbox){
			((Checkbox)obj).setVisible(bool);
		}else if(obj instanceof Intbox){			
			((Intbox)obj).setVisible(bool);
		}else if(obj instanceof Doublebox){			
			((Doublebox)obj).setVisible(bool);	
		}else if(obj instanceof Decimalbox){			
			((Decimalbox) obj).setVisible(bool);
		}else if(obj instanceof Spinner){			
			((Spinner) obj).setVisible(bool);
		}else if(obj instanceof Captcha){
			((Captcha) obj).setVisible(bool);			
		}else if(obj instanceof Row){
			((Row) obj).setVisible(bool);			
		}else if(obj instanceof Button){
			((Button) obj).setVisible(bool);			
		}else if(obj instanceof Grid){
			((Grid) obj).setVisible(bool);			
		}else System.out.println("The Object no support!!");		
	}
	
	public static void setObjectDisabled(Object obj,boolean bool){
		if(obj instanceof Datebox){
			((Datebox)obj).setDisabled(bool);
		}else if(obj instanceof Textbox){
			((Textbox)obj).setDisabled(bool);
		}else if(obj instanceof Listbox){
			((Listbox)obj).setDisabled(bool);			
		}else if(obj instanceof Radio){
			((Radio)obj).setDisabled(bool);			
		}else if(obj instanceof Checkbox){
			((Checkbox)obj).setDisabled(bool);
		}else if(obj instanceof Intbox){			
			((Intbox)obj).setDisabled(bool);
		}else if(obj instanceof Doublebox){			
			((Doublebox)obj).setDisabled(bool);
		}else if(obj instanceof Decimalbox){			
			((Decimalbox) obj).setDisabled(bool);
		}else if(obj instanceof Spinner){			
			((Spinner) obj).setDisabled(bool);
		}else if(obj instanceof Button){			
			((Button) obj).setDisabled(bool);			
		}else System.out.println("The Object no support Disabled Attribute!!");
	}	
	
	public static String getObjectValue(Listitem item,int a) {
		return strManage.changeString(((Listcell)item.getChildren().get(a)).getLabel().trim());
	}

	public static Listitem addObjectChild(Object obj,Object fieldValue,Object fieldLabel) {
		if(obj instanceof Listbox){
			Listbox tempObj = (Listbox) obj;
			tempObj.appendItem((String)fieldLabel,(String)fieldValue);
			return tempObj.getItemAtIndex(tempObj.getItemCount()-1);
		}else System.out.println("The Object no support");
		return null;
	}	
	
	public static int getObjectIndex(Object obj){
		if(obj instanceof Listbox) if(((Listbox)obj).getItemCount()>0) return ((Listbox)obj).getSelectedIndex();
		else if(obj instanceof Radiogroup) if(((Radiogroup)obj).getItemCount()>0) return ((Radiogroup)obj).getSelectedIndex();		
		return -1;
	}
	
	public static void setObjectIndex(Object obj,int index){
		if(obj instanceof Listbox){
			if(((Listbox)obj).getItemCount()>0){
				((Listbox)obj).setSelectedIndex(index);
			}
		}else if(obj instanceof Radiogroup){
			((Radiogroup)obj).getItemAtIndex(index).setChecked(true);
		}
	}	
	
	public static int getObjectCount(Object obj){
		if(obj instanceof Listbox) ((Listbox)obj).getItemCount();
		else if(obj instanceof Radiogroup) ((Radiogroup)obj).getItemCount();		
		return -1;		
	}
	
	public static void ListInit(List tempList,Listbox listObject,String valueFieldName,String labelFieldName,Object attributeName,String toContrast,boolean showFirst){
		if(tempList.size()>0){
			String lbl="請選擇";
			String val="";
			int x=0;
			if(toContrast==null) toContrast="";
//			if(showFirst) listObject.appendItem(lbl,val);			
			if(toContrast.length()==0 & showFirst) listObject.appendItem(lbl,val);
			for(int a=0;a<tempList.size();a++){
				Map form=(Map)tempList.get(a);
				if(toContrast.length()==0 || (form.get(labelFieldName)+"").indexOf(toContrast)>-1){
					x++;
					Listitem tempObj = listObject.appendItem(form.get(labelFieldName)+"",form.get(valueFieldName)+"");
					if(attributeName!=null){
						if(attributeName instanceof List){
							List temp=(List)attributeName;
							for(int b=0;b<temp.size();b++) tempObj.setAttribute(temp.get(b)+"", form.get(temp.get(b)+"")+"");
						}else if(attributeName instanceof Vector){
							Vector temp=(Vector)attributeName;
								for(int b=0;b<temp.size();b++) tempObj.setAttribute(temp.get(b)+"", form.get(temp.get(b)+""));						
						}else if(attributeName instanceof String[]){
							String temp[]=(String[])attributeName;
							for(int b=0;b<temp.length;b++) tempObj.setAttribute(temp[b], form.get(temp[b]));						
						}else System.out.println("The Object no support !!");
					}
				}
			}
			if(listObject.getItemCount()==0& showFirst) listObject.appendItem(lbl,val);
			else listObject.setSelectedIndex(0);
//			if(listObject.getItemCount() > 1 & showFirst) listObject.setSelectedIndex(1);
		}
	}	
	
}
